package com.ydl.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ydl.common.util.ResultVo;
import com.ydl.dao.UserDao;
import com.ydl.entry.dto.UserDto;
import com.ydl.entry.pojo.User;
import com.ydl.entry.vo.user.*;
import lombok.RequiredArgsConstructor;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

import java.util.logging.Logger;


@Service
@RequiredArgsConstructor
public class UserService {

    private final UserDao userDao;
    private static final Logger log = Logger.getLogger(UserService.class.getName());

    public ResultVo login(LoginParam loginParam) {

        User user = userDao.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserName, loginParam.getUserName()));

        if (user == null) {
            return ResultVo.error("用户不存在");
        }

        if (user.getPassword().equals(loginParam.getPassword())) {
            return ResultVo.success("登录成功", new LoginResult().setUserId(user.getUserId()).setToken("1234325645FFFjava"));
        } else {
            return ResultVo.error("登录失败，用户名或密码错误！");
        }
    }

    public ResultVo userList(QueryUserParam queryUserParam) {
        List<User> users = userDao.selectList(new LambdaQueryWrapper<User>()
                .like(queryUserParam.getUserName() != null, User::getUserName, queryUserParam.getUserName())
                .like(queryUserParam.getPhone() != null, User::getPhone, queryUserParam.getPhone())
        );

        return ResultVo.success("查询成功！", users);

    }

    public ResultVo deleteUser(DeleteUserParam deleteUserParam) {

        int one = userDao.deleteById(deleteUserParam.getUserId());

        return ResultVo.success();

    }

    public ResultVo updateUser(UpdateUserParam updateUserParam) {

        int i = userDao.updateById(new User().setUserId(updateUserParam.getUserId()).setUserName(updateUserParam.getUserName()).setAge(updateUserParam.getAge()).setPhone(updateUserParam.getPhone()).setEmail(updateUserParam.getEmail()).setAddress(updateUserParam.getAddress()));

        return ResultVo.success("更新成功");
    }

    public ResultVo addUser(AddUserParam addUserParam) {

        int insert = userDao.insert(new User().setUserName(addUserParam.getUserName()).setAge(addUserParam.getAge()).setPhone(addUserParam.getPhone()).setEmail(addUserParam.getEmail()).setAddress(addUserParam.getAddress()).setPassword("123456"));

        return ResultVo.success();

    }

    public ResultVo output() throws FileNotFoundException {
        List<User> users = userDao.selectList(null);

        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\admin\\Documents\\easyexcel-export-user1.xlsx"));

        EasyExcel.write(fileOutputStream, User.class).sheet("用户信息").doWrite(users);

        return ResultVo.success();
    }

    public ResultVo fileInput(MultipartFile file) throws IOException {
        List<User> users = EasyExcel.read(file.getInputStream()).head(User.class).excelType(ExcelTypeEnum.XLSX).sheet(0).doReadSync();

        String jsonString = JSON.toJSONString(users);


        System.out.println(jsonString);


        return ResultVo.success();

    }

    public ResultVo logout() {
        return ResultVo.success();
    }
}
